﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ImaginationFactory.Dialogs
{
    /// <summary>
    /// SmallPainter.xaml 的交互逻辑
    /// </summary>
    public partial class SmallPainter : Window
    {
        public SmallPainter()
        {
            InitializeComponent();
        }

        private enum Shapes
        {
            Circle,
            Ellipse,
            Rectangle,
            Line
        }

        private int diameter = 8;               // set diameter of cirle
        private Brush brushColor = Brushes.Red; // set the drawing color
        private bool shouldErase = false;       // specify whether to erase
        private bool shouldPaint = false;       // specify whether to paint
        private Point lButtonDownPos;           // note the mouse left button down pos
        private Point lButtonUpPos;             // note the mouse left button up  pos

        private Shapes currentShape = Shapes.Circle; // set the drawing shape

        #region 画图函数
        void PaintCircle(Brush color, Point pos)
        {
            Ellipse newEllipse = new Ellipse();
            newEllipse.Fill = color;
            newEllipse.Width = diameter;
            newEllipse.Height = diameter;

            // set the ellipse's postion
            Canvas.SetLeft(newEllipse, pos.X);
            Canvas.SetTop(newEllipse, pos.Y);

            paintCanvas.Children.Add(newEllipse);
        }

        void PaintEllipse(Brush color, Point pos)
        {
            Ellipse newEllipse = new Ellipse();
            newEllipse.Fill = color;
            newEllipse.Width = diameter * 2;
            newEllipse.Height = diameter;

            // set the ellipse's postion
            Canvas.SetLeft(newEllipse, pos.X);
            Canvas.SetTop(newEllipse, pos.Y);

            paintCanvas.Children.Add(newEllipse);
        }

        void PaintRectangle(Brush color, Point pos)
        {
            Rectangle rect = new Rectangle();
            rect.Fill = color;
            rect.Width = diameter * 2;
            rect.Height = diameter;

            Canvas.SetLeft(rect, pos.X);
            Canvas.SetTop(rect, pos.Y);

            paintCanvas.Children.Add(rect);
        }

        private bool isNewLine = true;
        void PaintLine(Brush color, Point pos)
        {
            if (shouldPaint)
            {
                Line newLine = new Line();
                newLine.StrokeThickness = 5;
                newLine.Stroke = brushColor;
                newLine.X1 = 0;
                newLine.Y1 = 0;
                newLine.X2 = pos.X - lButtonDownPos.X;
                newLine.Y2 = pos.Y - lButtonDownPos.Y;

                Canvas.SetLeft(newLine, lButtonDownPos.X);
                Canvas.SetTop(newLine, lButtonDownPos.Y);

                if (!isNewLine)
                {
                    paintCanvas.Children.RemoveAt(paintCanvas.Children.Count - 1);
                }
                isNewLine = false;

                paintCanvas.Children.Add(newLine);
            }
        }


        private void PaintToCanvas(Brush color, Point pos, Shapes shape)
        {
            switch (shape)
            {
                case Shapes.Circle:
                    PaintCircle(color, pos);
                    break;
                case Shapes.Ellipse:
                    PaintEllipse(color, pos);
                    break;
                case Shapes.Rectangle:
                    PaintRectangle(color, pos);
                    break;
                case Shapes.Line:
                    PaintLine(color, pos);
                    break;
            }
        }
        #endregion

        #region 鼠标事件处理函数
        private void paintCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            lock (lineMutex)
            {

                if (shouldPaint)
                {
                    PaintToCanvas(brushColor, e.GetPosition(paintCanvas), currentShape);
                }
                else if (shouldErase)
                {
                    PaintToCanvas(paintCanvas.Background, e.GetPosition(paintCanvas), currentShape);
                }
            }
        }

        Object lineMutex = new object();
        private void paintCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            lock (lineMutex)
            {
                shouldPaint = true;
                lButtonDownPos = e.GetPosition(paintCanvas);
                isNewLine = true;  // sets the flag that it is a new line
            }
        }

        private void paintCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            shouldPaint = false;
            lButtonUpPos = e.GetPosition(paintCanvas);
        }

        private void paintCanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            shouldErase = true;
        }

        private void paintCanvas_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            shouldErase = false;
        }
        #endregion

        #region 颜色选择
        private void BlueChosen(object sender, RoutedEventArgs e)
        {
            brushColor = Brushes.Blue;
        }

        private void RedChosen(object sender, RoutedEventArgs e)
        {
            brushColor = Brushes.Red;
        }

        private void BlackChosen(object sender, RoutedEventArgs e)
        {
            brushColor = Brushes.Black;
        }
        #endregion

        #region 形状选择
        private void RectangleChosen(object sender, RoutedEventArgs e)
        {
            currentShape = Shapes.Rectangle;
        }

        private void EllipseChosen(object sender, RoutedEventArgs e)
        {
            currentShape = Shapes.Ellipse;
        }

        private void CircleChosen(object sender, RoutedEventArgs e)
        {
            currentShape = Shapes.Circle;
        }

        private void LineChosen(object sender, RoutedEventArgs e)
        {
            currentShape = Shapes.Line;
        }

        #endregion

        #region 按钮
        //
        // 撤销上一步
        //
        //
        private void ResetButton_Click(object sender, RoutedEventArgs e)
        {
            paintCanvas.Children.RemoveAt(paintCanvas.Children.Count - 1);
        }

        //
        // 清空
        //
        //
        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            paintCanvas.Children.Clear();
        }
        #endregion 
    }
}
